package com.platform.system.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.device.SpeakerTypeDto;
import com.platform.system.domain.entity.device.SpeakerType;
import com.platform.system.domain.vo.device.SpeakerTypeVo;
import com.platform.system.mapper.device.SpeakerTypeMapper;
import com.platform.system.service.ISysSpeakerTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class SysSpeakerTypeServiceImpl extends ServiceImpl<SpeakerTypeMapper, SpeakerType> implements ISysSpeakerTypeService {

    @Resource
    SpeakerTypeMapper speakerTypeMapper;

    @Resource
    ISysSpeakerTypeService sysSpeakerTypeService;

    @Override
    public TableDataInfo selectSpeakerTypeList(SpeakerTypeVo speakerInfoVo) {
        LambdaQueryWrapper<SpeakerType> speakerTypeLambdaQueryWrapper = new QueryWrapper<SpeakerType>().lambda();
        speakerTypeLambdaQueryWrapper.like(StringUtils.isNotEmpty(speakerInfoVo.getStyleName()), SpeakerType::getStyleName, speakerInfoVo.getStyleName());
        speakerTypeLambdaQueryWrapper.eq(speakerInfoVo.getType() != null, SpeakerType::getType, speakerInfoVo.getType());
        speakerTypeLambdaQueryWrapper.orderByDesc(SpeakerType::getCreateTime);
        Page<SpeakerType> page = new Page<>();
        page.setCurrent(speakerInfoVo.getPageNum());
        page.setSize(speakerInfoVo.getPageSize());
        IPage<SpeakerType> speakerTypeIPage = speakerTypeMapper.selectPage(page, speakerTypeLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        List<SpeakerType> list =  speakerTypeIPage.getRecords();
        List<SpeakerTypeDto> speakerTypeDtoList = new ArrayList<>();
        if(list != null || list.size() > 0){
            String array = JSONArray.toJSONString(list);
            speakerTypeDtoList = JSONArray.parseArray(array, SpeakerTypeDto.class);
        }
        rspData.setRows(speakerTypeDtoList);
        rspData.setTotal(speakerTypeIPage.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        return rspData;
    }

    @Override
    public List<SpeakerType> selectSpeakerTypeByUidList(List<String> uidList) {
        LambdaQueryWrapper<SpeakerType> speakerTypeLambdaQueryWrapper = new QueryWrapper<SpeakerType>().lambda();
        speakerTypeLambdaQueryWrapper.in(SpeakerType::getUid, uidList);
        List<SpeakerType> speakerTypeList = speakerTypeMapper.selectList(speakerTypeLambdaQueryWrapper);
        return speakerTypeList;
    }

    @Override
    public SpeakerTypeDto selectSpeakerType(String uid) {
        LambdaQueryWrapper<SpeakerType> speakerTypeLambdaQueryWrapper = new QueryWrapper<SpeakerType>().lambda();
        speakerTypeLambdaQueryWrapper.eq(SpeakerType::getUid, uid);
        SpeakerType speakerType = speakerTypeMapper.selectOne(speakerTypeLambdaQueryWrapper);
        if(speakerType != null) {
            SpeakerTypeDto speakerTypeDto = new SpeakerTypeDto();
            BeanUtils.copyProperties(speakerType, speakerTypeDto);
            return speakerTypeDto;
        }
        return null;
    }

    @Override
    public SpeakerType selectSpeakerTypeByName(SpeakerTypeVo speakerTypeVo) {
        LambdaQueryWrapper<SpeakerType> speakerTypeLambdaQueryWrapper = new QueryWrapper<SpeakerType>().lambda();
        speakerTypeLambdaQueryWrapper.eq(SpeakerType::getStyleName, speakerTypeVo.getStyleName());
        SpeakerType speakerType = speakerTypeMapper.selectOne(speakerTypeLambdaQueryWrapper);
        return speakerType;
    }

    @Override
    public AjaxResult insertSpeakerType(SpeakerTypeVo speakerTypeVo) {
        SpeakerType speakerType = new SpeakerType();
        speakerType.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        speakerType.setStyleName(speakerTypeVo.getStyleName());
        speakerType.setRemark(speakerTypeVo.getRemark());
        speakerType.setType(speakerTypeVo.getType());
        speakerType.setCreateTime(new Date());
        speakerType.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteSpeakerType(SpeakerTypeVo speakerTypeVo) {
        LambdaQueryWrapper<SpeakerType> speakerTypeLambdaQueryWrapper = new QueryWrapper<SpeakerType>().lambda();
        speakerTypeLambdaQueryWrapper.eq(SpeakerType::getUid, speakerTypeVo.getUid());
        speakerTypeMapper.delete(speakerTypeLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateSpeakerType(SpeakerTypeVo speakerTypeVo) {
        SpeakerTypeDto speakerTypeDto = sysSpeakerTypeService.selectSpeakerType(speakerTypeVo.getUid());
        if(speakerTypeDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        SpeakerType getSpeakerType = sysSpeakerTypeService.selectSpeakerTypeByName(speakerTypeVo);
        if(getSpeakerType != null && !getSpeakerType.getUid().equals(speakerTypeVo.getUid())){
            throw new BusinessException(ResultEnum.NAME_EXIST);
        }
        LambdaQueryWrapper<SpeakerType> speakerTypeLambdaQueryWrapper = new QueryWrapper<SpeakerType>().lambda();
        speakerTypeLambdaQueryWrapper.eq(SpeakerType::getUid, speakerTypeVo.getUid());
        SpeakerType speakerType = new SpeakerType();
        BeanUtils.copyProperties(speakerTypeVo, speakerType);
        speakerType.setUpdateTime(new Date());
        speakerType.update(speakerTypeLambdaQueryWrapper);
        return AjaxResult.success();
    }
}
